Attribute VB_Name = "mdlFunctions"
Option Explicit

Public Type RecieveMessage
    ContactName As String
    ContactEmail As String
    Content As String
    Recieving As Boolean
    Length As Integer
End Type

Public Type Buddy
    FriendlyName As String
    Email As String
    Status As String
    Group As Integer
End Type

Public Type BuddyList
    totalGroups As Integer
    totalBuddies As Integer
    Groups() As String
    Buddies() As Buddy
End Type

Public message As RecieveMessage
Public List As BuddyList

Public trID_a As Integer

Function HexToDecColor(Hexa As String) As Long
    Dim Red As Integer
    Dim Green As Integer
    Dim Blue As Integer
    
    Red = Val("&H" & Left(Hexa, 2))
    Green = Val("&H" & Mid(Hexa, 3, 2))
    Blue = Val("&H" & Right(Hexa, 2))
    
    HexToDecColor = RGB(Red, Green, Blue)
End Function

Sub Log(Str)

    If debugOpts = True Then
        Debug.Print Str
        frmMain.txtLog.Text = frmMain.txtLog.Text & Str & vbCrLf
        frmMain.txtLog.SelStart = Len(frmMain.txtLog.Text)
    End If

End Sub

Sub Connect()
    On Error Resume Next
    frmMain.sckMain.Close
    frmMain.lblName = "Connecting..."
    Log "Connecting..."
    Dim controla
    For Each controla In frmMain.Controls
        If controla.Tag = "x" Then
            controla.Enabled = False
        End If
    Next
    frmMain.sckMain.Connect defServer, defPort
    
    budCount = 0

End Sub

Sub Send_Command(Str)

    frmMain.sckMain.SendData Str & vbCrLf
    Log "Out: " & Str

End Sub

Function trID()

    trID_a = trID_a + 1
    trID = trID_a
    
End Function

Function Urldecode(Str As String)
    Dim i As Integer
    Dim res As String

    For i = 1 To Len(Str)
        If Mid$(Str, i, 1) = "%" Then
            res = res & Chr(Val("&H" & Mid$(Str, i + 1, 2)))
            i = i + 3
        End If
        res = res & Mid$(Str, i, 1)
    Next
    
    Urldecode = DecodeUTF8(res)
    
End Function

Function Urlencode(Str As String)

    Dim Allowed As String
    Dim res As String
    Dim i As Integer
    
    Allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+."
    
    For i = 1 To Len(Str)
        If InStr(Allowed, Mid$(Str, i, 1)) Then
            res = res & Mid(Str, i, 1)
        Else
            res = res & "%" & Format(Hex(Asc(Mid$(Str, i, 1))), "00")
        End If
    Next
    
    Urlencode = res

End Function
